<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>elevator algorithm</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div style="position: fixed;top: 100px;left: 10px;">
    floor: <input id="floor" type="text" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();"/>
    <button onclick="add()">add</button>
</div>
<div id="main"></div>
<script type="text/javascript" src="http://yhzheng.com/webfire/js/obj.js"></script>
<script>
    var floor = $("#floor");
    var ele = new role("#fa0",100,200,"color",screen.width / 2 - 50,100,0,"ele","main");
    var text = "This is elevator.<br/>This is an elevator algorithm demonstration<br/>It's a lot better than some elevator algorithms.";
    var floors = [];
    ele.showShadow(true,5,5,"#666");
    ele.setText(text + "<br/>floor:1","#fff",10,"bold","center");
    ele.define("floor",1);
    ele.MOVE = function (floor) {
        ele.setText(text + "<br/>floor:" + ele.floor,"#fff",10,"bold","center");
        setTimeout(function () {
            ele.floor = floor;
            ele.move(screen.width / 2 - 50,2100 - (floor * 210) + 100);
            ele.setText(text + "<br/>floor:" + floor,"#fff",10,"bold","center");
        },2000);
    };
    function add() {
        if (floor.val() === "")
            return;
        floors.push(Number(floor.val()));
        floor.val("");
        $(document).trigger("floorsAdd");
        $(document).trigger("floorsChange");
    }
    $(document).on("floorsChange",function () {
        for (var i in floors){
            for (var j = 0 ; j < (floors.length - 1) ; j++){
                if (floors[j] > floors[j + 1]){
                    var tmp = floors[j];
                    floors[j] = floors[j + 1];
                    floors[j + 1] = tmp;
                }
            }
        }
    });
    $(document).on("floorsAdd",function () {
        for (var i in floors){
            if (floors[i] > ele.floor){
                ele.MOVE(floors[i]);
                floors.splice(i,i + 1);
                $(document).trigger("floorsChange");
            }
        }
        for (var i in floors){ele.MOVE(
            floors[i]);
            floors.splice(i,i + 1);
        }
    });
</script>
</body>
</html>